/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.TableCell
 *  javafx.scene.control.TableColumn
 *  javafx.scene.control.TableView
 *  javafx.scene.control.TextField
 *  javafx.scene.control.cell.PropertyValueFactory
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.AnchorPane
 *  javafx.stage.Modality
 *  javafx.stage.Stage
 *  javafx.util.Callback
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.salesReceipts;

import cn.hutool.core.util.ObjectUtil;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.salesReceipts.GiftPdAndExpController;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.salesReceipts.GetSalesReceiptsTableOutData;
import com.gys.data.salesReceipts.GiftData;
import com.gys.view.salesReceipts.GiftPdAndExpView;
import com.gys.view.salesReceipts.GiftView;
import de.felixroske.jfxsupport.FXMLController;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class GiftController
extends BaseController
implements Initializable {
    private static final Logger log = LoggerFactory.getLogger(GiftController.class);
    @FXML
    private AnchorPane pane;
    @FXML
    private TableView<GiftData> tableView;
    @FXML
    private TableColumn<GiftData, String> promotionType;
    @FXML
    private TableColumn<GiftData, String> giftCode;
    @FXML
    private TableColumn<GiftData, String> giftNormalPrice;
    @FXML
    private TableColumn<GiftData, String> giftName;
    @FXML
    private TableColumn<GiftData, String> proFactoryName;
    @FXML
    private TableColumn<GiftData, String> promotionContent;
    @FXML
    private TableColumn<GiftData, String> index;
    @FXML
    private TableColumn<GiftData, String> giveNum;
    @FXML
    private TableColumn<GiftData, String> giveNumHave;
    @FXML
    private TableColumn<GiftData, String> giveNumReal;
    @FXML
    private TableColumn<GiftData, String> addPrice;
    @FXML
    private TableColumn<GiftData, String> proPym;
    @FXML
    private TableColumn<GiftData, String> proSpecs;
    @FXML
    private TableColumn<GiftData, String> promotionName;
    @FXML
    private TableColumn<GiftData, String> addDisc;
    @FXML
    private TableColumn<GiftData, String> param;
    @FXML
    private TableColumn<GiftData, String> voucherId;
    @FXML
    private TableColumn<GiftData, String> barcode;
    @FXML
    private TableColumn<GiftData, String> giftStock;
    @FXML
    private TextField giftParam;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    private boolean flag = false;
    @Autowired
    private GiftPdAndExpController giftPdAndExpController;
    public ObservableList<GiftData> observableTableList = FXCollections.observableArrayList(new ArrayList());
    public ObservableList<GiftData> observableTableListFinal = FXCollections.observableArrayList(new ArrayList());
    public List<GiftData> giftData = new ArrayList<GiftData>();

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.init();
        this.addTextField(this.giveNumReal);
        this.bingDing();
    }

    @Override
    public void init() {
        GetSalesReceiptsTableOutData item;
        int i;
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                GiftView view = SpringUtil.getBean(GiftView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.F1) {
                            GiftController.this.giftParamAction();
                        } else if (e.getCode() == KeyCode.ENTER) {
                            GiftController.this.sureAction();
                        } else if (e.getCode() == KeyCode.ESCAPE) {
                            GiftController.this.cancelAction();
                        }
                        e.consume();
                    }
                });
            }
        }, 1000L);
        this.observableTableList.clear();
        this.observableTableListFinal.clear();
        this.tableView.setItems(this.observableTableList);
        this.tableView.refresh();
        this.initTable();
        ObservableList items = this.salesReceiptsMainController.getTableView().getItems();
        ObservableList remove = FXCollections.observableArrayList(new ArrayList());
        for (i = 0; i < items.size(); ++i) {
            item = (GetSalesReceiptsTableOutData)items.get(i);
            if (!"1".equals(item.getIsGift())) continue;
            remove.add(item);
        }
        items.removeAll((Collection)remove);
        for (i = 0; i < items.size(); ++i) {
            item = (GetSalesReceiptsTableOutData)items.get(i);
            item.setIndex(String.valueOf(i + 1));
        }
        this.salesReceiptsMainController.getTableView().refresh();
    }

    private void initTable() {
        this.observableTableList = FXCollections.observableArrayList(this.giftData);
        this.observableTableListFinal = FXCollections.observableArrayList(this.giftData);
        this.tableView.setItems(this.observableTableList);
        this.tableView.refresh();
    }

    private void bingDing() {
        this.promotionType.setCellValueFactory((Callback)new PropertyValueFactory(this.promotionType.getId()));
        this.giftCode.setCellValueFactory((Callback)new PropertyValueFactory(this.giftCode.getId()));
        this.giftNormalPrice.setCellValueFactory((Callback)new PropertyValueFactory(this.giftNormalPrice.getId()));
        this.giftName.setCellValueFactory((Callback)new PropertyValueFactory(this.giftName.getId()));
        this.proFactoryName.setCellValueFactory((Callback)new PropertyValueFactory(this.proFactoryName.getId()));
        this.promotionContent.setCellValueFactory((Callback)new PropertyValueFactory(this.promotionContent.getId()));
        this.index.setCellValueFactory((Callback)new PropertyValueFactory(this.index.getId()));
        this.giveNum.setCellValueFactory((Callback)new PropertyValueFactory(this.giveNum.getId()));
        this.giveNumHave.setCellValueFactory((Callback)new PropertyValueFactory(this.giveNumHave.getId()));
        this.giveNumReal.setCellValueFactory((Callback)new PropertyValueFactory(this.giveNumReal.getId()));
        this.addPrice.setCellValueFactory((Callback)new PropertyValueFactory(this.addPrice.getId()));
        this.proPym.setCellValueFactory((Callback)new PropertyValueFactory(this.proPym.getId()));
        this.proSpecs.setCellValueFactory((Callback)new PropertyValueFactory(this.proSpecs.getId()));
        this.promotionName.setCellValueFactory((Callback)new PropertyValueFactory(this.promotionName.getId()));
        this.addDisc.setCellValueFactory((Callback)new PropertyValueFactory(this.addDisc.getId()));
        this.param.setCellValueFactory((Callback)new PropertyValueFactory(this.param.getId()));
        this.voucherId.setCellValueFactory((Callback)new PropertyValueFactory(this.voucherId.getId()));
        this.barcode.setCellValueFactory((Callback)new PropertyValueFactory(this.barcode.getId()));
        this.giftStock.setCellValueFactory((Callback)new PropertyValueFactory(this.giftStock.getId()));
    }

    private void addTextField(TableColumn<GiftData, String> column) {
        column.setCellFactory(tc -> {
            final TextField textField = new TextField();
            TableCell<GiftData, String> cell = new TableCell<GiftData, String>(){

                protected void updateItem(String chuzhi, boolean empty) {
                    super.updateItem(chuzhi, empty);
                    if (empty) {
                        this.setGraphic(null);
                    } else {
                        int index = this.getIndex();
                        if (ObjectUtil.isNotEmpty(chuzhi)) {
                            GiftController.this.flag = true;
                            textField.setText(chuzhi);
                            GiftController.this.flag = false;
                        }
                        this.setGraphic((Node)textField);
                    }
                }
            };
            textField.textProperty().addListener((ChangeListener)new ChangeListener<String>(){
                final  TableCell val_cell;
                final  TextField val_textField;
                {
                    this.val_cell = cell;
                    this.val_textField = textField;
                }

                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    if (GiftController.this.flag) {
                        return;
                    }
                    int index = this.val_cell.getIndex();
                    GiftData giftData = (GiftData)GiftController.this.observableTableList.get(index);
                    if (ObjectUtil.isNotEmpty(newValue) && !CommonUtil.positiveInteger(newValue)) {
                        this.val_textField.setText(oldValue);
                        return;
                    }
                    this.val_textField.setText(newValue);
                    giftData.setGiveNumReal(newValue);
                    boolean re = GiftController.this.count(giftData);
                    if (!re) {
                        GiftController.this.flag = true;
                        this.val_textField.setText(oldValue);
                        GiftController.this.flag = false;
                        return;
                    }
                    ObservableList remove = FXCollections.observableArrayList(new ArrayList());
                    for (int i = 0; i < GiftController.this.observableTableList.size(); ++i) {
                        GiftData giftData1 = (GiftData)GiftController.this.observableTableList.get(i);
                        if (!"0".equals(giftData1.getGiveNum())) continue;
                        remove.add(giftData1);
                    }
                    GiftController.this.observableTableList.removeAll((Collection)remove);
                }
            });
            return cell;
        });
    }

    private boolean count(GiftData giftData) {
        GiftData giftDataTemp;
        int i;
        int count = 0;
        for (i = 0; i < this.observableTableList.size(); ++i) {
            giftDataTemp = (GiftData)this.observableTableList.get(i);
            if (!giftData.getFlag().equals(giftDataTemp.getFlag()) || ObjectUtil.isEmpty(giftDataTemp.getGiveNumReal())) continue;
            count += Integer.valueOf(giftDataTemp.getGiveNumReal()).intValue();
        }
        if (count > Integer.valueOf(giftData.getGiveNumD())) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8d85\u51fa\u6700\u5927\u53ef\u9001\u6570\u91cf");
            alert.showAndWait();
            return false;
        }
        if (ObjectUtil.isNotEmpty(giftData.getGiveNumReal()) && Integer.valueOf(giftData.getGiveNumReal()) > Integer.valueOf(giftData.getGiftStock())) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8d85\u51fa\u5e93\u5b58\u6570\u91cf");
            alert.showAndWait();
            return false;
        }
        for (i = 0; i < this.observableTableList.size(); ++i) {
            giftDataTemp = (GiftData)this.observableTableList.get(i);
            if (!giftData.getFlag().equals(giftDataTemp.getFlag())) continue;
            giftDataTemp.setGiveNumHave(String.valueOf(count));
            int giveNum = Integer.valueOf(giftDataTemp.getGiveNumD()) - count;
            giftDataTemp.setGiveNum(String.valueOf(giveNum));
            String indexD = giftDataTemp.getIndexD();
            GiftData giftDataFinal = (GiftData)this.observableTableListFinal.get(Integer.valueOf(indexD) - 1);
            giftDataFinal.setGiveNumHave(String.valueOf(count));
            giftDataFinal.setGiveNum(String.valueOf(giveNum));
        }
        return true;
    }

    @FXML
    private void giftParamAction() {
        String text = this.giftParam.getText();
        if (ObjectUtil.isEmpty(text)) {
            GiftData giftData;
            int i;
            HashSet<String> indexSet = new HashSet<String>();
            this.observableTableList = FXCollections.observableArrayList(this.observableTableListFinal);
            for (int i2 = 0; i2 < this.observableTableListFinal.size(); ++i2) {
                GiftData giftData1 = (GiftData)this.observableTableListFinal.get(i2);
                if (!"0".equals(giftData1.getGiveNum())) continue;
                indexSet.add(giftData1.getIndexD());
            }
            ObservableList remove = FXCollections.observableArrayList(new ArrayList());
            for (i = 0; i < this.observableTableList.size(); ++i) {
                giftData = (GiftData)this.observableTableList.get(i);
                if (!indexSet.contains(giftData.getIndexD())) continue;
                remove.add(giftData);
            }
            this.observableTableList.removeAll((Collection)remove);
            for (i = 0; i < this.observableTableList.size(); ++i) {
                giftData = (GiftData)this.observableTableList.get(i);
                giftData.setIndex(String.valueOf(i + 1));
            }
            this.tableView.setItems(this.observableTableList);
            this.tableView.refresh();
            return;
        }
        this.observableTableList.clear();
        int indexTemp = 0;
        block5: for (int i = 0; i < this.observableTableListFinal.size(); ++i) {
            GiftData giftData = (GiftData)this.observableTableListFinal.get(i);
            Class<?> cls = giftData.getClass();
            Field[] fields = cls.getDeclaredFields();
            for (int j = 0; j < fields.length; ++j) {
                Field f = fields[j];
                try {
                    if (f.getClass() == Class.forName("javafx.beans.property.SimpleStringProperty")) continue;
                    f.setAccessible(true);
                    String value = (String)f.get(giftData);
                    if (!value.contains(text)) continue;
                    giftData.setIndex(String.valueOf(indexTemp + 1));
                    this.observableTableList.add(giftData);
                    continue block5;
                }
                catch (Exception e) {
                    log.info("\u51fa\u9519\u5c5e\u6027\u540d:" + f.getName());
                    e.printStackTrace();
                }
            }
        }
        this.tableView.refresh();
    }

    @FXML
    private void sureAction() {
        for (int i = 0; i < this.observableTableListFinal.size(); ++i) {
            GiftData giftData = (GiftData)this.observableTableListFinal.get(i);
            if (ObjectUtil.isEmpty(giftData.getGiveNumReal()) || "0".equals(giftData.getGiveNumReal())) continue;
            this.giftPdAndExpController.giftData = giftData;
            this.giftPdAndExpController.init();
            CommonUtil.showView(GiftPdAndExpView.class, Modality.APPLICATION_MODAL);
        }
        this.cancelAction();
    }

    @FXML
    private void cancelAction() {
        this.pane.getScene().getWindow().hide();
    }
}

